package ge.telasi.tasks.ui.user;

import ge.telasi.tasks.model.Group;
import ge.telasi.tasks.model.User;
import ge.telasi.tasks.ui.Platform;
import ge.telasi.tasks.ui.comp.TableModel;
import ge.telasi.tasks.ui.UIUtils;
import ge.telasi.tasks.ui.log.Loggable;
import ge.telasi.tasks.ui.log.Logger;
import ge.telasi.tasks.ui.log.LoggerUtils;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.table.TableColumnModel;

/**
 * @author dimitri
 */
public class GroupUserTable extends JTable implements Loggable {
    static final int USERNAME = 0;
    static final int FULLNAME = 1;

    private Group group;
    private Logger logger;
    
    public GroupUserTable() {
        setModel(new UserTableModel());
        TableColumnModel colModel = getColumnModel();
        colModel.getColumn(USERNAME).setPreferredWidth(120);
        colModel.getColumn(FULLNAME).setPreferredWidth(200);
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() > 1) {
                    User user = getSelectedUser();
                    if (user != null) {
                        UserPropertiesDialog dialog = new UserPropertiesDialog(GroupUserTable.this);
                        dialog.setUser(user);
                        UIUtils.openDialog(dialog);
                    }
                }
            }
        });
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    @Override
    public Logger getLogger() {
        return logger;
    }

    @Override
    public void setLogger(Logger logger) {
        this.logger = logger;
    }

    public void refresh() {
        UserTableModel model = (UserTableModel) getModel();
        if (group != null && Platform.getDefault().validateConnection()) {
            try {
                Platform pl = Platform.getDefault();
                List<User> users = pl.getFacade().getGroupUsers(pl.getCredentials(), group);
                model.setData(users);
            } catch (Exception ex) {
                LoggerUtils.manageException(this, ex);
            }
        } else if (group == null) {
            model.setData(new ArrayList<User>());
        }
    }

    public void add() {
        if (group != null && Platform.getDefault().validateConnection()) {
            UserPickDialog dialog = new UserPickDialog(this);
            dialog.refresh();
            UIUtils.openDialog(dialog);
            if (dialog.isApproved()) {
                Platform pl = Platform.getDefault();
                try {
                    pl.getFacade().addUsersToGroup(pl.getCredentials(), group, dialog.getSelectedUsers());
                    refresh();
                } catch (Exception ex) {
                    LoggerUtils.manageException(this, ex);
                }
            }
        }
    }

    public void remove() {
        if (group != null && Platform.getDefault().validateConnection()) {
            List<User> users = getSelectedUsers();
            Platform pl = Platform.getDefault();
            String msg = pl.getLocaleManager().getMessage("General.deleteByCountMsg", users.size());
            if (UIUtils.askConfirmation(this, msg)) {
                try {
                    pl.getFacade().removeUsersFromGroup(pl.getCredentials(), group, users);
                    refresh();
                } catch (Exception ex) {
                    LoggerUtils.manageException(this, ex);
                }
            }
        }
    }

    public User getSelectedUser() {
        int index = getSelectedRow();
        if (index != -1) {
            UserTableModel model = (UserTableModel) getModel();
            return model.getObjectAt(index);
        }
        return null;
    }

    public List<User> getSelectedUsers() {
        int[] indecies = getSelectedRows();
        if (indecies != null && indecies.length > 0) {
            UserTableModel model = (UserTableModel) getModel();
            List<User> users = new ArrayList<User>();
            for (int index : indecies) {
                User user = model.getObjectAt(index);
                users.add(user);
            }
            return users;
        }
        return null;
    }

    @Override
    public JComponent[] allFields() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    class UserTableModel extends TableModel<User> {

        @Override
        public int getColumnCount() {
            return 2;
        }

        @Override
        public String getColumnName(int column) {
            switch (column) {
                case USERNAME:
                    return "მომხმ. სახელი";
                case FULLNAME:
                    return "ნამდვილი სახელი";
                default:
                    return "?";
            }
        }

        @Override
        public Class<?> getColumnClass(int column) {
            switch (column) {
                case USERNAME:
                case FULLNAME:
                    return String.class;
                default:
                    return Object.class;
            }
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            switch (column) {
                default:
                    return false;
            }
        }

        @Override
        public Object getValueAt(int row, int column) {
            User user = getObjectAt(row);
            switch (column) {
                case USERNAME:
                    return user.getUsername();
                case FULLNAME:
                    return user.getFullName();
                default:
                    return "?";
            }
        }

    }

}
